/**
 * <p>Title: UserStringUtil</p>
 * <p>Descriptions: </p>
 * <p>Company:星海</p>
 * @author 陈刚
 * @date 2017-4-5 上午10:15:10
 */

package com.cg.javafxdemospring.util;

import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.Map.Entry;


/**
 * <p>Title: UserStringUtil</p>
 * <p>Descriptions: </p>
 * <p>Company:星海</p>
 * @author 陈刚
 * @date 2017-4-5 上午10:15:10
 */

public class UserStringUtil {
	
	public static final String EMPTY = "";
	
	public static final String POINT = ".";
	
	/**
	 * sqlTemplate1 = " AND FIND_IN_SET('${0}',GROUP_CONCAT(cp._propertyId))\n";
	 */
	public static final String sqlTemplate1 = " AND FIND_IN_SET('${0}',GROUP_CONCAT(cp._propertyId))\n";
	/**
	 * sqlTemplate2 = " AND FIND_IN_SET('${0}',GROUP_CONCAT(tcp._propertyId))\n";
	 */
	public static final String sqlTemplate2 = " AND FIND_IN_SET('${0}',GROUP_CONCAT(tcp._propertyId))\n";
	/**
	 * sqlTemplate3 = " AND FIND_IN_SET('${0}',GROUP_CONCAT(tech_catalogproperties._propertyId))\n";
	 */
	public static final String sqlTemplate3 = " AND FIND_IN_SET('${0}',GROUP_CONCAT(tech_catalogproperties._propertyId))\n";
	
	/**
	 * sqlTemplate4 = " AND FIND_IN_SET('${0}',tc._propertiesCache)\n";
	 */
	public static final String sqlTemplate4 = " AND FIND_IN_SET('${0}',tc._propertiesCache)\n";

	/**
	 * sqlTemplate5 = " AND FIND_IN_SET('${0}',ttc._propertiesCache)\n";
	 */
	public static final String sqlTemplate5 = " AND FIND_IN_SET('${0}',ttc._propertiesCache)\n";
	/**
	 * sqlTemplate6 = " AND FIND_IN_SET('${0}',ttc._propertiesCache)\n";
	 */
	public static final String sqlTemplate6 = " AND FIND_IN_SET('${0}',tech_catalogproperties._propertiesCache)\n";
	
	public static final String sqlTemplate7 =" AND FIND_IN_SET('${0}',tut._propertiesCache)\n";
	
	/**
	 * 
	 * <p>Title: getSuffix</p>
	 * <p>Descriptions: 获取文件后缀</p>
	 * @author 陈刚
	 * @date 2017-9-12 上午10:35:00
	 * @param 
	 * @return String
	 */
	public static String getSuffix(String path){
		if (path == null || EMPTY.equals(path.trim())) {
			return EMPTY;
		}
		if (path.contains(POINT)) {
			return path.substring(path.lastIndexOf(POINT) + 1, path.length());
		}
		return EMPTY;
	}
	
	/**
	 * 
	 * <p>Title: getFileName</p>
	 * <p>Descriptions: 获取文件名</p>
	 * @author 陈刚
	 * @date 2017-9-12 上午10:35:58
	 * @param 
	 * @return String
	 */
	public static String getFileName(String path){
		if (path == null || EMPTY.equals(path.trim())) {
			return EMPTY;
		}
		if (path.contains(POINT)) {
			return path.substring(path.lastIndexOf(File.pathSeparator)==-1?0:path.lastIndexOf(File.pathSeparator), path.lastIndexOf(POINT));
		}
		return EMPTY;
	}
	
	/**
	 * 
	 * <p>Title: getFileName</p>
	 * <p>Descriptions: 获取文件的路径，不包含文件名</p>
	 * @author 陈刚
	 * @date 2017-9-12 上午10:35:58
	 * @param 
	 * @return String
	 */
	public static String getFilePath(String path){
		if (StringUtils.isEmpty(path)) {
			return EMPTY;
		}
		if (path.contains(File.separator)) {
			return path.substring(0,path.lastIndexOf(File.separator));
		}
		return EMPTY;
	}
	
	/**
	 * <p>testgetFilePath:()</p>
	 * @description 
	 * @author 陈刚
	 * @date 2019年1月4日 上午11:51:35 
	 */
	public void testgetFilePath() {
		System.out.println("-----");
		System.out.println(getFilePath("d:\\a.doc"));

	}
	
	
	public static long getSystemTime(){
		Date date = new Date();
		return date.getTime();
	}
	
	public static Object[] removeArrayHead(Object[] obj){
		if(obj==null) {
            return null;
        } else if(obj.length==1) {
            return new Object[0];
        }
		Object[] newObj = new String[obj.length-1];
		for (int i = 0; i < newObj.length; i++) {
			newObj[i] = obj[i+1];
		}
		return newObj;
	}
	
	public static Object[] addArrayHead(Object[] obj,Object head){
		if(obj==null || obj.length==0) {
            return new Object[]{head};
        }
		Object[] newObj = new Object[obj.length+1];
		newObj[0] = head;
		for (int i = 0; i < obj.length; i++) {
			newObj[i+1] = obj[i];
		}
		return newObj;
	}
	
	
	public static String[] removeArrayHead(String[] str){
		if(str==null) {
            return null;
        } else if(str.length==1) {
            return new String[0];
        }
		String[] newStr = new String[str.length-1];
		for (int i = 0; i < newStr.length; i++) {
			newStr[i] = str[i+1];
		}
		return newStr;
	}
	
	public static String[] addArrayHead(String[] str,String head){
		if(str==null || str.length==0) {
            return new String[]{head};
        }
		String[] newStr = new String[str.length+1];
		newStr[0] = head;
		for (int i = 0; i < str.length; i++) {
			newStr[i+1] = str[i];
		}
		return newStr;
	}
	
	
	public static void printArray(String[] str){
		for (String string : str) {
			System.out.println(string);
		}
	}
	
	public static String getISO8859_1(String str){
		try {
			str = new String(str.getBytes(),"ISO8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}
	
	/**
	 * <p>uncapFirst:(首字母小写)</p>
	 * <p>(适用条件 - 可选)</p>
	 * <p>(执行流程 - 可选)</p>
	 * <p>(使用方法 - 可选)</p>
	 * <p>(注意事项 - 可选)</p>
	 * @author 陈刚
	 * @date 2018年5月16日 上午10:46:10 
	 */
	public static String uncapFirst(String name) {
		return name.substring(0,1).toLowerCase()+name.substring(1);
	}
	
	/**
	 * <p>uncapFirst:(首字母大写)</p>
	 * <p>(适用条件 - 可选)</p>
	 * <p>(执行流程 - 可选)</p>
	 * <p>(使用方法 - 可选)</p>
	 * <p>(注意事项 - 可选)</p>
	 * @author 陈刚
	 * @date 2018年5月16日 上午10:46:10 
	 */
	public static String capFirst(String name) {
		return name.substring(0,1).toUpperCase()+name.substring(1);
	}
	
	/**
	 * <p>getUrlParam:()</p>
	 * <p>(适用条件 - 可选)</p>
	 * <p>(执行流程 - 可选)</p>
	 * <p>(使用方法 - 可选)</p>
	 * <p>(注意事项 - 可选)</p>
	 * @author 陈刚
	 * @date 2018年7月11日 下午8:16:25 
	 */
	public static String getUrlParamValue(String url,String paramName) {
		if (paramName==null) {
			return "";
		}
		String params = url.substring(url.indexOf("?")+1, url.length());
		String[] paramsArray = params.split("&");
		for (String param : paramsArray) {
			String[] paramKV = param.split("=");
			for (int i = 0; i < paramKV.length; i++) {
				if(paramName.equals(paramKV[0])){
					if (paramKV.length>1) {
						return paramKV[1];
					}else {

						return "";
					}
				}
			}
			
		}
		return "";
	}

	/**
	 * <p>getFindinsetWhereByProperties:()</p>
	 * @description 
	 * @author 陈刚
	 * @date 2018年8月25日 下午2:54:15
	 * @param properties 
	 */
	public static String getFindinsetWhereByProperties(String properties,String sqlTemplate) {
		String result = "";
		if (StringUtils.isEmpty(properties)) {
			return "";
		}
		String[] propertiesArray = properties.split(";");
		List<List<String>> propertyListList = new ArrayList<List<String>>(); 
		for (String string : propertiesArray) {
			List<String> list = Arrays.asList(string.split(","));
			propertyListList.add(list);
		}
		List<List<String>> cartesianProduct = UserStringUtil.getCartesianProduct(propertyListList);
		result += " AND ( 0 ";
		for (List<String> list : cartesianProduct) {
			result += " OR ( 1=1 ";
			for (String string : list) {
				result += sqlTemplate.replace("${0}", string);
			}
			result +=")";
		}
		result += ")";
		return result;
	}
	
	public static void recursive(List<List<String>> dimValue, List<List<String>> result, int layer, List<String> curList) {
		if (layer < dimValue.size() - 1) {
			if (dimValue.get(layer).size() == 0) {
				recursive(dimValue, result, layer + 1, curList);
			} else {
				for (int i = 0; i < dimValue.get(layer).size(); i++) {
					List<String> list = new ArrayList<String>(curList);
					list.add(dimValue.get(layer).get(i));
					recursive(dimValue, result, layer + 1, list);
				}
			}
		} else if (layer == dimValue.size() - 1) {
			if (dimValue.get(layer).size() == 0) {
				result.add(curList);
			} else {
				for (int i = 0; i < dimValue.get(layer).size(); i++) {
					List<String> list = new ArrayList<String>(curList);
					list.add(dimValue.get(layer).get(i));
					result.add(list);
				}
			}

		}

	}
	
	public static void recursiveUseObject(List<List<?>> dimValue, List<List<?>> result, int layer, List<?> curList) {
		if (layer < dimValue.size() - 1) {
			if (dimValue.get(layer).size() == 0) {
				recursiveUseObject(dimValue, result, layer + 1, curList);
			} else {
				for (int i = 0; i < dimValue.get(layer).size(); i++) {
					List<Object> list = new ArrayList<Object>(curList);
					list.add(dimValue.get(layer).get(i));
					recursiveUseObject(dimValue, result, layer + 1, list);
				}
			}
		} else if (layer == dimValue.size() - 1) {
			if (dimValue.get(layer).size() == 0) {
				result.add(curList);
			} else {
				for (int i = 0; i < dimValue.get(layer).size(); i++) {
					List<Object> list = new ArrayList<Object>(curList);
					list.add(dimValue.get(layer).get(i));
					result.add(list);
				}
			}

		}

	}
	/**
	 * <p>getCartesianProduct:()</p>
	 * @description 获取笛卡尔积
	 * @author 陈刚
	 * @date 2018年8月25日 下午3:33:29 
	 */
	public static List<List<String>> getCartesianProduct(List<List<String>> dimValue) {
		List<List<String>> result = new ArrayList<List<String>>();
		recursive(dimValue, result, 0, new ArrayList<String>());
		return result;
	}
	
	/**
	 * <p>getCartesianProduct:()</p>
	 * @description 获取笛卡尔积
	 * @author 陈刚
	 * @date 2018年8月25日 下午3:33:29 
	 */
	public static List<List<?>> getCartesianProductUseObject(List<List<?>> dimValue) {
		List<List<?>> result = new ArrayList<List<?>>();
		recursiveUseObject(dimValue, result, 0, new ArrayList<>());
		return result;
	}
	
	/**
	 * <p>getCartesianProduct:()</p>
	 * @description 获取笛卡尔积
	 * @author 陈刚
	 * @date 2018年8月25日 下午3:33:29 
	 */
	@SuppressWarnings("unchecked")
	public static List<List<String>> getCartesianProduct(String properties) {
		if (StringUtils.isEmpty(properties)) {
			return Collections.EMPTY_LIST;
		}
		String[] propertiesArray = properties.split(";");
		List<List<String>> propertyListList = new ArrayList<List<String>>(); 
		for (String string : propertiesArray) {
			List<String> list = Arrays.asList(string.split(","));
			propertyListList.add(list);
		}
		List<List<String>> result = new ArrayList<List<String>>();
		recursive(propertyListList, result, 0, new ArrayList<String>());
		return result;
	}
	
	public static void main(String[] args) {
		/*List<List<String>> result = new ArrayList<>();
		List<List<String>> sList = new ArrayList<>();
		List<String> a = Arrays.asList("1","2","3");
		List<String> b = Arrays.asList("4","5","6");
		List<String> c = Arrays.asList("7","8","9");
		sList.add(a);sList.add(b);sList.add(c);
		recursive(sList, result, 0, new ArrayList<>());
		for (List<String> list : result) {
			for (String string : list) {
				System.out.print(string+",");
			}
			System.out.println();
		}*/
		String aresult = getFindinsetWhereByProperties("1,2,3;4,5,6;7,8,9"," AND FIND_IN_SET('${0}',GROUP_CONCAT(_id))");
		System.out.println(aresult);
	}
	

	
	public static Integer[] getIntegersByString(String string){
		if (StringUtils.isEmpty(string)) {
			return new Integer[0];
		}
		String[] strings = string.split(",");
		Integer[] result = new Integer[strings.length];
		for (int i = 0; i < strings.length; i++) {
			result[i] = Integer.valueOf(strings[i]);
		}
		return result;
	} 
	
	public static Integer[] getIntegersByStringArray(String[] strings){
		if (strings==null || strings.length==0) {
			return new Integer[0];
		}
		Integer[] result = new Integer[strings.length];
		for (int i = 0; i < strings.length; i++) {
			result[i] = Integer.valueOf(strings[i]);
		}
		return result;
	} 
	
	/**
	 * <p>getUnicode:()</p>
	 * @description 将单个字符转成unicode编码
	 * @author 陈刚
	 * @date 2018年10月29日 上午9:44:52
	 * @param string
	 * @return 
	 */
	public static String getUnicode(String string) {
		char[] chars = "　".toCharArray();
		String str = "";
		for (int i = 0; i < chars.length; i++) {
			str = "\\u" + Integer.toString(chars[i], 16);
		}
		return str;
	}
	
	public static String getASCII(){
		String ascii = "";
		for (int i = 33; i < 127; i++) {
			ascii+=(char)i;
		}
		return ascii;
	}
	
	/**
	 * <p>test:()</p>
	 * @description 
	 * @author 陈刚
	 * @date 2019年5月5日 下午7:53:21 
	 */
	public void testASCII() {
		System.out.println(" "+getASCII()+" ");
		
		List<List<?>> dimValue = new ArrayList<List<?>>();
		dimValue.add(new ArrayList<String>(){{
			add("4");
			add("5");
			add("6");
		}});
		dimValue.add(new ArrayList<Object>(){{
			add(1);
			add(2);
			add(3);
		}});
		
		System.out.println(JSONArray.fromObject(getCartesianProductUseObject(dimValue)));
	}
	
	/**
	 * <p>replace:()</p>
	 * @description 
	 * @author 陈刚
	 * @date 2019年8月14日 上午10:05:33 
	 */
	public static String replace(String source,Map<String, String> repaceMap) {
		for (Entry<String, String> entry : repaceMap.entrySet()) {
			source = source.replace(entry.getKey(), entry.getValue());
		}
		return source;
	}
	
	
	public static String replace(String source,Object... strings) {
		if (strings==null||strings.length<=1) {
			return source;
		}
		for (int i = 0; i < strings.length; i+=2) {
			source = source.replace(strings[i].toString(), strings[i+1].toString());
		}
		return source;
	}
	public static <T> String join(T[] array,String separator) {
		if(array.length==0) {
            return "";
        }
		if(array.length==1) {
            return String.valueOf(array[0]);
        }
		StringBuilder sb = new StringBuilder();
		sb.append(array[0]);
		for(int i=1;i<array.length;i++) {
			sb.append(separator);
			sb.append(array[0]);
		}
		return sb.toString();
	}
	public static <T> String join(T[] array,char separator) {
		if(array.length==0) {
            return "";
        }
		if(array.length==1) {
            return String.valueOf(array[0]);
        }
		StringBuilder sb = new StringBuilder();
		sb.append(array[0]);
		for(int i=1;i<array.length;i++) {
			sb.append(separator);
			sb.append(array[0]);
		}
		return sb.toString();
	}
	public static <T> String join(T[] array) {
		return join(array,',');
	}
}
